// pages/user-my-order-evaluate-medical/user-my-order-evaluate-medical.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.globalData.imgUrl,          // 线上图片
    localImg: app.globalData.staticimgUrl,  // 本地图片
    commentcontent: "", //评价内容
    goodsid: "", //商品ID
    images: [], //图片
    orderid: "", //订单ID
    fs: 5, //打分
    route1: "", //上传后的图片
    imagesurl: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      goodsid: options.id, //商品ID
      orderid: options.orderId, //订单id
      orderType: options.orderType, //订单id
    })
  },
  //打分
  choose: function(e) {
    this.setData({
      fs: e.currentTarget.dataset.current,
    });
  },
  //保存内容
  save: function(e) {
    var that = this
    //内容
    this.setData({
      commentcontent: e.detail.value
    });
  },

  //选择图片待上传
  chooseImg: function(e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      count:1,
      success: function(res) {
        // 将现有和新上传的合并
        var images = that.data.images.concat(res.tempFilePaths);
        images = images.length <= 1 ? images : images.slice(0, 1);
        that.setData({
          images: images,
        })

        that.uploadimages(); // 选择图片后 上传到服务器上
      },
    })
  },

  // 上传图片
  uploadimages: function(e) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    });
    var images = this.data.images;
    var imagesArr = [];
    for (var i = 0; i < images.length; i++) {
    
      var aa = images[i];
      app.uploadFile({
        url: 'shopp/goodsCommentApi/uploadCommentImage',
        filePath: images[i],
        success: function(res) {
          var path = JSON.parse(res.data).data;
          var str = JSON.stringify(path);
          var arr = "[" + str + "]";
          imagesArr.push(arr);
          that.setData({
            route1: imagesArr
          })
        }
      })
    };
  },

  // 删除图片
  deleteimg: function(e) {
    var index = e.target.dataset.index;
    var imagesurl = this.data.imagesurl;
    var images = this.data.images;
    imagesurl.splice(index, 1);
    images.splice(index, 1);
    this.setData({
      'imagesurl': imagesurl,
      'images': images,
    });
  },

  //提交评价 
  evaluate: function() {
    var that = this;
    app.request({
      url: 'shopp/goodsCommentApi/addComment',
      data: {
        commentcontent: that.data.commentcontent, //评价内容
        goodsid: that.data.goodsid, //商品ID
        images: that.data.route1, //图片
        orderid: that.data.orderid, //订单ID
        score: that.data.fs //打分
      },
     
      success(res) {
        wx.navigateTo({
          url: '../user-my-order-list-ping/user-my-order-list-ping?id=' + that.data.orderid + '&orderType=' + that.data.orderType,
        })
        wx.showToast({
          title: '商品评价成功',
          icon: 'success',
          duration: 1000
        })

      }
    })
  }
})